package com.zbcbms.controller;

import com.zbcbms.common.AlertUtilCommon;
import com.zbcbms.common.GetChartCommon;
import com.zbcbms.common.TableInitCommon;
import com.zbcbms.Main;
import com.zbcbms.entity.dto.LoaderDto;
import com.zbcbms.entity.dto.UserMap;
import com.zbcbms.entity.tableData.BookBorrowTableData;
import com.zbcbms.entity.tableData.BookTableData;
import com.zbcbms.entity.tableData.UserTableData;
import com.zbcbms.service.impl.BookServiceImpl;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.StackedBarChart;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import java.net.URL;
import java.util.ResourceBundle;


/**
 * @author fengq
 */
public class HomePageController implements Initializable {

    @FXML
    private TableView<BookTableData> searchTableView;
    @FXML
    private TableColumn<BookTableData, String> searchTableCode;
    @FXML
    private TableColumn<BookTableData, String> searchTableBookName;
    @FXML
    private TableColumn<BookTableData, String> searchTableAuthor;
    @FXML
    private TableColumn<BookTableData, String> searchTablePublishName;
    @FXML
    private TableColumn<BookTableData, String> searchTableYear;
    @FXML
    private TableColumn<BookTableData, String> searchTablePrice;
    @FXML
    private TableColumn<BookTableData, String> searchTableCategory;
    @FXML
    private TableColumn<BookTableData, String> searchTableStatus;
    @FXML
    private AnchorPane mainFrameAnchorPane;
    @FXML
    private AnchorPane tempFrameAnchorPane;

    private GetChartCommon chartCommon=new GetChartCommon();

    ObservableList<BookTableData> data = new BookServiceImpl().getBookTableViewData();
    @FXML
    private TextField searchText;

    @FXML
    private PieChart pieChartBookType;
    @FXML
    private TitledPane bookManagePane;
    @FXML
    private TitledPane userManagePane;
    @FXML
    private TitledPane borrowManagePane;
    @FXML
    private TitledPane recycleManagePane;

    @FXML
    private StackedBarChart<String,Number> bookStatusStackChart;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        authorityJudge();
        new TableInitCommon().homeTableInit(searchTableView,searchTableCode,searchTableBookName,searchTableStatus,
                searchTableAuthor,searchTablePublishName,searchTableYear,searchTablePrice,searchTableCategory,
                data);
        bookStatusStackChart.setData(chartCommon.getBorrowTopStackedBarChart());
        pieChartBookType.setData(chartCommon.getBookTypeChartData());

    }



    private void resetAnchorPane() {
        mainFrameAnchorPane.getChildren().clear();
        mainFrameAnchorPane.getChildren().add(tempFrameAnchorPane);
    }

    public void do_queryUserMenuItem_event(ActionEvent event) {
        tempFrameAnchorPane = new Main().initMainFrame("/view/QueryUser.fxml");
        resetAnchorPane();
    }

    public void do_toHomePage(ActionEvent event) {
        tempFrameAnchorPane = new Main().initMainFrame("/view/MainTable.fxml");
        resetAnchorPane();
    }

    public void do_toSelfBorrow(ActionEvent event) {
        tempFrameAnchorPane = new Main().initMainFrame("/view/SelfBorrowing.fxml");
        resetAnchorPane();
    }

    public void do_toUpdatePerson(ActionEvent event) {
        tempFrameAnchorPane = new Main().initMainFrame("/view/UpdatePerson.fxml");
        resetAnchorPane();
    }

    public void do_addUserMenuItem_event(ActionEvent event) {
        tempFrameAnchorPane = new Main().initMainFrame("/view/AddUser.fxml");
        resetAnchorPane();
    }
    public void do_addBookMenuItem_event(ActionEvent event) {
        tempFrameAnchorPane = new Main().initMainFrame("/view/AddBook.fxml");
        resetAnchorPane();
    }
    public void do_addBorrowingMenuItem_event(ActionEvent event) {
        tempFrameAnchorPane = new Main().initMainFrame("/view/AddBorrowing.fxml");
        resetAnchorPane();
    }
    public void do_queryBookMenuItem_event(ActionEvent event) {
        tempFrameAnchorPane = new Main().initMainFrame("/view/QueryBook.fxml");
        resetAnchorPane();
    }

    public void do_queryBorrowingMenuItem_event(ActionEvent event) {
        tempFrameAnchorPane = new Main().initMainFrame("/view/QueryBorrowing.fxml");
        resetAnchorPane();
    }

    public void do_recycleBookMenuItem_event(ActionEvent event) {
        tempFrameAnchorPane = new Main().initMainFrame("/view/RecycleBook.fxml");
        resetAnchorPane();
    }

    public void do_recycleBorrowingMenuItem_event(ActionEvent event) {
        tempFrameAnchorPane = new Main().initMainFrame("/view/RecycleBorrow.fxml");
        resetAnchorPane();
    }

    public void do_recycleUserMenuItem_event(ActionEvent event) {
        tempFrameAnchorPane = new Main().initMainFrame("/view/RecycleUser.fxml");
        resetAnchorPane();
    }

    public void do_toLoginMenuItem_event(ActionEvent event) {
        Boolean aBoolean = new AlertUtilCommon().AlterMessageOption(mainFrameAnchorPane.getScene().getWindow(), "是否要退出本系统？",
                "确认退出", "取消退出", "系统提示", "小仲书：主人是否真的要离开？");
        if (aBoolean){
            Stage stage=(Stage) mainFrameAnchorPane.getScene().getWindow();
            stage.close();
            UserMap.setName(null);
            UserMap.setNumber(null);
            UserMap.setPassword(null);
            UserMap.setRole(null);
            new Main().start(stage);
        }

    }

    public void do_toUpdateUser(UserTableData userTableData){
        LoaderDto loaderDto = new Main().UpdateMainFrame("/view/UpdateUser.fxml");
        tempFrameAnchorPane=loaderDto.getAnchorPane();
        resetAnchorPane();
        UpdateUserController controller = loaderDto.getLoader().getController();
        controller.UserMessageInit(userTableData);
    }

    public void do_toUpdateBook(BookTableData bookTableData){
        LoaderDto loaderDto = new Main().UpdateMainFrame("/view/UpdateBook.fxml");
        tempFrameAnchorPane=loaderDto.getAnchorPane();
        resetAnchorPane();
        UpdateBookController controller = loaderDto.getLoader().getController();
        controller.updateTextInit(bookTableData);
    }

    public void do_toUpdateBookBorrow(BookBorrowTableData bookBorrowTableData){
        LoaderDto loaderDto = new Main().UpdateMainFrame("/view/UpdateBorrowing.fxml");
        tempFrameAnchorPane=loaderDto.getAnchorPane();
        resetAnchorPane();
        UpdateBorrowController controller = loaderDto.getLoader().getController();
        controller.updateTextInit(bookBorrowTableData);
    }

    public void setMainFrameAnchorPane(AnchorPane mainFrameAnchorPane) {
        this.mainFrameAnchorPane = mainFrameAnchorPane;
    }

    @FXML
    public void searchBtnAction(){
        String text = searchText.getText();
        new TableInitCommon().homeTableInit(searchTableView,searchTableCode,searchTableBookName,searchTableStatus,
                searchTableAuthor,searchTablePublishName,searchTableYear,searchTablePrice,searchTableCategory,
                new BookServiceImpl().getBookVoByKeyWord(text));

    }

    public void authorityJudge(){
        String role = UserMap.getRole();
        if (role.equals("图书管理员")){
            userManagePane.getStyleClass().add("hide-pane");
        }else if(role.equals("借阅者")){
            bookManagePane.getStyleClass().add("hide-pane");
            userManagePane.getStyleClass().add("hide-pane");
            recycleManagePane.getStyleClass().add("hide-pane");
            borrowManagePane.getStyleClass().add("hide-pane");
        }
    }





}
